<template>
  <div class="app-container">
    <!-- 顶部搜索 -->
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="100px">
      <el-form-item label="科研项目名称" prop="kyXmName">
        <el-input size="mini"
                  v-model="queryParams.kyXmName"
                  placeholder="请输入科研项目名称"
                  clearable
                  @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="年度" prop="nd">
        <el-select size="mini" v-model="queryParams.nd" placeholder="请选择年度" clearable>
          <el-option
            v-for="dict in dict.type.year"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="依托工程" prop="ytGc">
        <el-input size="mini"
                  v-model="queryParams.ytGc"
                  placeholder="请输入依托工程"
                  clearable
                  @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="甲方单位" prop="jfDw">
        <el-select size="mini" v-model="queryParams.jfDw" placeholder="请选择甲方单位" clearable>
          <el-option
            v-for="dict in dict.type.xmgl_lxwj_pzdw"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="承担单位" prop="cdDw">
        <el-input size="mini"
                  v-model="queryParams.cdDw"
                  placeholder="请输入承担单位"
                  clearable
                  @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="是否开票" prop="sfKp">
        <el-select size="mini" v-model="queryParams.sfKp" placeholder="请选择是否开票" clearable>
          <el-option
            v-for="dict in dict.type.sf"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="合同状态" prop="htZt">
        <el-select v-model="queryParams.htZt" placeholder="请选择合同状态" size="mini" clearable>
          <el-option
            v-for="dict in dict.type.wtww_htzt"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <!-- 顶部按钮 -->
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['wtww:wtww:export']"
        >导出
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <!-- 表格 -->
    <el-table @row-click="clickRow" ref="table" highlight-current-row
              :header-cell-style="{background:'#d0cece',color:'#000000',fontWeight:'bold'}"
              v-loading="loading" :data="wtwwList" @selection-change="handleSelectionChange">
      <!--      <el-table-column type="selection" width="55" align="center"/>-->
      <el-table-column label="序号" align="center" type="index" width="50"/>
      <el-table-column label="年度" align="center" prop="nd" show-overflow-tooltip>
        <template slot-scope="scope">
          <dict-tag :options="dict.type.year" :value="scope.row.nd"/>
        </template>
      </el-table-column>
      <el-table-column label="科研项目名称" align="center" prop="kyXmName" show-overflow-tooltip>
        <template slot-scope="scope">
          <el-button
            type="text"
            @click="handleButtonClick(scope.row)"
          >
            <el-link type="primary">{{ scope.row.kyXmName }}</el-link>
          </el-button>
        </template>
      </el-table-column>
      <el-table-column label="项目类型" align="center" prop="xmLx" show-overflow-tooltip/>
      <el-table-column label="依托工程" align="center" prop="ytGc" show-overflow-tooltip/>
      <el-table-column label="甲方单位" align="center" prop="jfDw" show-overflow-tooltip>
        <template slot-scope="scope">
          <dict-tag :options="dict.type.xmgl_lxwj_pzdw" :value="scope.row.jfDw"/>
        </template>
      </el-table-column>
      <el-table-column label="承担单位" align="center" prop="cdDw" show-overflow-tooltip/>
      <el-table-column label="合同金额(元)" align="center" prop="htJe" show-overflow-tooltip/>
      <el-table-column label="是否开票" align="center" prop="sfKp" width="80" show-overflow-tooltip>
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sf" :value="scope.row.sfKp"/>
        </template>
      </el-table-column>
      <el-table-column label="开始时间" align="center" prop="ksTime" width="150" show-overflow-tooltip>
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.ksTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="结束时间" align="center" prop="jsTime" width="150" show-overflow-tooltip>
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.jsTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="合同状态" align="center" prop="htZt" show-overflow-tooltip>
        <template slot-scope="scope">
          <dict-tag :options="dict.type.wtww_htzt" :value="scope.row.htZt"/>
        </template>
      </el-table-column>
      <el-table-column label="锁定状态" align="center" prop="sdZt" width="80" show-overflow-tooltip/>
      <el-table-column label="录入人" align="center" prop="createBy" width="80" show-overflow-tooltip/>
    </el-table>

    <!-- 分页组件 -->
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 委托委外详情组件 -->
    <WtWwXq ref="wtWwXq"/>
  </div>
</template>

<script>
import {
  addWtww,
  delWtww, delWtwwFj,
  getWtww,
  getWtWwId,
  insertWtwwFjByWtwwId,
  listWtww,
  selectWtwwFjList, selectWtwwKxJlByWtwwId, updateSdztLocking, updateSdztUnlock,
  updateWtww
} from "@/api/xmgl/wtww/wtww";
import WtWwDr from "@/views/xmgl/wtww/wtwwgl/components/WtWwDr";
import {getToken} from "@/utils/auth";
import KjryDx from "@/views/components/kjryXz/kjryDx";
import WtWwXq from "@/views/xmgl/wtww/wtwwgl/components/WtWwXq";

export default {
  name: "wtWwCx", //委托委外查询
  components: {WtWwXq, KjryDx, WtWwDr},
  dicts: ['xmgl_lxwj_pzdw', 'year', 'wtww_htzt', 'sf', 'sys_user_sex'],
  data() {
    return {
      wtwwFileType: "合同文档", //默认的附件类型
      defaultProps: {children: "children", label: "label"}, // 表单参数
      fileOptions: [{ //附件类型树表
        id: 1,
        label: '1.合同文档',
        type: '合同文档'
      }, {
        id: 2,
        label: '2.过程文档',
        type: '过程文档'
      }, {
        id: 3,
        label: '3.验收文档',
        type: '验收文档'
      }, {
        id: 4,
        label: '4.其他',
        type: '其他'
      }],
      xg: false, //判断是新增还是修改 (false新增 true修改)
      allFilesUploaded: false, // 添加这个来追踪所有文件是否上传完成
      fjUpload: false, //附件上传弹窗
      //要修改的 委托委外id
      wtwwIdEdit: "",
      uploadFileUrl: process.env.VUE_APP_BASE_API + "/common/upload", // 上传文件服务器地址
      fileList: [],
      headers: {Authorization: "Bearer " + getToken()},
      number: 0,
      uploadList: [],
      fileType: ["doc", "xls", "ppt", "txt", "pdf", "docx", "zip"],
      selectedRow: null, // 保存当前选中的行
      getXmLx: null, //用户点击新增后选择的项目类型
      getWtWwId: null, //从后端获取的委托委外主键ID
      activeName: 'jbzl', //初始打开的标签页
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 子表选中数据
      checkedWtwwFj: [],
      // 款项记录子表选中数据
      checkedWtwwKxjl: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 委托委外表格数据
      wtwwList: [],
      // 委托委外附件资料表格数据
      wtwwFjList: [],
      // 款项记录表格数据
      wtwwKxjlList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        xmLx: null,
        sdZt: null,
        kyXmName: null,
        nd: null,
        ytGc: null,
        jfDw: null,
        cdDw: null,
        sfKp: null,
        htZt: null,
        userId: null,
        deptId: null
      },
      loadingFj: false,
    };
  },
  created() {
    this.getList();
  },
  methods: {
    //打开委托委外详情
    handleButtonClick(row) {
      this.$refs.wtWwXq.handleOpen(row);
    },
    // 点击行后选中该行
    clickRow(row) {
      this.selectedRow = row; // 更新选中的行
      const table = this.$refs.table;
      const selectedRows = table.selection;

      // 否则取消之前选中的行，并选中当前点击的行
      selectedRows.forEach(selectedRow => {
        table.toggleRowSelection(selectedRow);
      });
      table.toggleRowSelection(row);
    },
    /** 查询委托委外列表 */
    getList() {
      this.loading = true;
      listWtww(this.queryParams).then(response => {
        this.wtwwList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      let ids = selection.map(item => item.wtwwId)
      if (ids.length === 1) {
        this.ids = ids[0]
      }else {
        this.ids = ids
      }
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('wtww/wtww/export', {
        ...this.queryParams
      }, `wtww_${new Date().getTime()}.xlsx`)
    },
  }
};
</script>
